import axios from 'axios';
import mockRouter from "./mockRouter"

export const post  = async (api, params = {},headers) => {
    const result = await axios( {
      url:api,
      method: 'post',
      params: {},
      headers:headers||{},
      data: params,
      getResponse: true,
      responseType: 'json', 

    });
  
   return result.data;
}


export const get = async (api, params = {},headers) => {
  const result = await axios( {
    url:api,
    method: 'get',
    params: params,
    headers:headers||{},
    data: {},
    getResponse: true,
    responseType: 'json', 

  });
  return result.data;
};

//远程mock
const get0=async (url,params,headers)=>{
  return  get(window.M.config.baseMockUrl(url),params,headers)
}
//本地mock
const get1=async (url,params,headers)=>{
    return  get(window.M.config.baseLocalMockUrl(mockRouter[url]),params,headers)
}

//将ajax请求挂到全局对象M上
window.M.request={}
window.M.request.get=get;
window.M.request.get0=get0;
window.M.request.get1=get1;
window.M.request.post=post;
window.M.request.reqByReq = async (url,req) => {
    if(req.method==="get"){
      let result=  await get(url,req.params,req.headers)
      return result;
    }
    if(req.method==="post"){
      let result= await post(url,req.params,req.headers)
      return result;
    }
};